package com.yuhs.zixing.study.multithread.mashibing.t002lock.l001;

import lombok.extern.slf4j.Slf4j;

/**
 * Function: synchronized
 * 对某个对象加锁
 *
 * @Author yuhongsheng
 * @Date: 2023-12-23 18:53:26
 */
@Slf4j
public class Lock001Synchronized001 {


    private int count = 10;
    private Object object = new Object();

    /**
     * object 锁
     */
    public void m1() {
        synchronized (object) {
            count--;
            log.atInfo().log("线程 {} count {}", Thread.currentThread().getName(), count);
        }
    }

    /**
     * this 锁
     */
    public void m2() {
        synchronized (this) {
            count--;
            log.atInfo().log("线程 {} count {}", Thread.currentThread().getName(), count);
        }
    }

    /**
     * 方法 锁  == synchronized (this)
     */
    public synchronized void m3() {
        count--;
        log.atInfo().log("线程 {} count {}", Thread.currentThread().getName(), count);

    }


}
